package com.webtest.jsptest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.ext.Locator2;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2018/7/24 10:14
 */
public class LoginServlet2 extends HttpServlet {
    private static final Logger LOGGER = LoggerFactory.getLogger(Locator2.class);
    private static final long serialVersionUID = -6004945847269091241L;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 先将ServletRequest转换为子接口HttpServletRequest
        String requestURI = req.getRequestURI();
        LOGGER.info(requestURI);

        String requestUrl = req.getRequestURL().toString();
        LOGGER.info("url:" + requestUrl);

        String method = req.getMethod();
        LOGGER.info("method:" + method);

        /*********** servletResponse ***********/
        // getWriter() 返回Printwriter对象，调用该对象的print()方法，将内容直接输入到response的body中，显示在客户端的浏览器中
        String user = req.getParameter("user");
        PrintWriter out = resp.getWriter();
        out.println("<h1>");
        if (user == null) {
            out.println("Response from server");
        } else {
            out.println("Hello " + user);
        }
        out.println("</h1>");

    }
}
